console.log("Logging: hello 01-1");
let storage = window.localStorage;
let list = JSON.parse(storage.getItem("list"));
let onlyNum = JSON.parse(storage.getItem("onlyNum"));
//在页面刷新问卷列表

function freshQuestionareList() {
    console.log("freshQuestionList");
    let questionareListObj = document.getElementById("questionareList");
    while(questionareListObj.firstChild){
        questionareListObj.removeChild(questionareListObj.firstChild);
    }
    if (list == null || list.length <= 0){
        list = [];
        storage.setItem("list",JSON.stringify(list));
    }
    if (onlyNum == null || onlyNum == ""){
        onlyNum = 1;
        storage.setItem("onlyNum",JSON.stringify(onlyNum));
    }
    if (list != null && list.length > 0) {
        for (var i = 0; i < list.length; i++) {
            let j = list[i].id;//j为取得value问卷的唯一key
            let data = storage.getItem(j);
            if (data != null || data != "") {
                let questionare = JSON.parse(data);
                freshQuestionare(j, questionare);
            }
        }
    }
}

 // 往页面更新单个问卷
function freshQuestionare(j,questionare) {
    console.log("freshQuestionare");
    let questionareListObj = document.getElementById("questionareList");
    let questionareObj = document.createElement("div");
    questionareObj.setAttribute("class","questionare");
    questionareObj.onmouseover = function () {
        questionareObj.className = "over";
    }
    questionareObj.onmouseout = function () {
        questionareObj.className = "out";
    }
    for (i = 1; i <= 5; i++){
        let divObj = document.createElement("div");
        if (i == 1){
            divObj.setAttribute("class","checkbox");
            let inputObj = document.createElement("input");
            inputObj.setAttribute("type","checkbox");
            inputObj.setAttribute("name","cb");
            inputObj.setAttribute("onclick","isCheckAll()");
            inputObj.setAttribute("value",j);
            divObj.appendChild(inputObj);
        }else if (i == 2){
            divObj.setAttribute("class","title");
            let pObj = document.createElement("p");
            divObj.appendChild(pObj);
            pObj.innerHTML += questionare.title;
        }else if (i == 3){
            divObj.setAttribute("class","time");
            let pObj = document.createElement("p");
            divObj.appendChild(pObj);
            pObj.innerHTML += questionare.deadline;
        }else if (i == 4){
            divObj.setAttribute("class","state");
            let pObj = document.createElement("p");
            if (questionare.state === "发布中"){
                divObj.setAttribute("class","stateGreen");
            }
            if (questionare.state === "已结束"){
                divObj.setAttribute("class","stateRed");
            }
            divObj.appendChild(pObj);
            pObj.innerHTML += questionare.state;
        }else if (i == 5){
            divObj.setAttribute("class","operation");
            let btnFullin = document.createElement("button");//填写
            let btnEdit = document.createElement("button");//编辑
            let btnDelete = document.createElement("button");//删除
            let btnView = document.createElement("button");//查看数据
            btnFullin.setAttribute("onclick","fillinQuestionare("+j+")");
            btnView.setAttribute("onclick","viewQuestionareData("+j+")");
            divObj.appendChild(btnFullin);
            divObj.appendChild(btnView);
            btnFullin.innerHTML += "填写";
            btnView.innerHTML += "查看数据";
        }
        questionareObj.appendChild(divObj);
    }
    questionareListObj.appendChild(questionareObj);
}



//填写问卷

function fillinQuestionare(i) {
    
    var state = JSON.parse(storage.getItem(i)).state;
    if (state === "发布中"){
        window.location.href = "./form copy.html?id="+i+"";
    }else if (state === "未发布"){
        alert("问卷未发布");
    }else if (state === "已结束"){
        alert("问卷已结束");
    }
}



//查看问卷数据
function viewQuestionareData(i) {
   
    window.location.href = "./dateview.html?id="+i+"";
}



// 移除localStorage的list[i]和等于i的key
function removeData(i) {

    storage.removeItem(i);
    if (list != null) {
        for (var j = 0; j < list.length; j++) {
            if (list[j].id == i) {
                list.splice(j, 1);
                storage.setItem("list",JSON.stringify(list));
                break;
            }
        }
    }
}

